<?php

namespace APP\Ctrl;
use CMS\Ctrl as Base;
use CMS\Helper as Helper;

class Metro extends Base {
	protected $layout = 'none';

	// construct
	public function run() {
		$this->blizzard();

		// manage uri paths
		if(count($this->uri) > 0) {
			@list($func) = $this->uri;
			$this->$func();
		}

		// send authorization token to socket.io
		if(isset($this->session->auth))
			$this->view->authorization = base64_encode(strlen($this->session->auth['id']) . $this->session->auth['id'] . $this->session->auth['password']);
	}

	// handle metros linked with blizzard api
	private function blizzard() {
		// get realm status
		$this->view->realm_status = false; $this->view->realm_queue = false;
		$realm = $this->db->querySingle("SELECT value FROM blizzard WHERE api = 'realm'");
		if($realm) {
			$realm = unserialize($realm);
			if($realm->realms[0]->status) $this->view->realm_status = true;
			if($realm->realms[0]->queue) $this->view->realm_queue = true;
		}

		// get guild member count
		$this->view->roster_count = false;
		$guild = $this->db->querySingle("SELECT value FROM blizzard WHERE api = 'guild'");
		if($guild) {
			$guild = unserialize($guild);
			$this->view->roster_count = count($guild->members);
		}
	}

	// logout
	public function logout() {
		// no session, just stop here
		if(!isset($this->session->auth))
			return $this->json = array('success' => false);

		// set the name as a reference before we destroy it
		$name = $this->session->auth['name'];

		// destroy session & cookie
		unset($this->session->auth);
		unset($this->cookie->auth);

		// success
		return $this->json = array('success' => true, 'user' => array(
			'name' => $name
		));
	}
}